﻿/*
 * Created by SharpDevelop.
 * User: JavCasta - http://javcasta.com/
 * Date: 27/10/2012
 * Time: 16:28
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace Sumarizar
{
	/// <summary>
	/// Description of MainForm.
	/// </summary>
	public partial class MainForm : Form
	{
		int lineas = 0;
		public MainForm()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			richTextBox1.Text = "10.56.248.0/24"+ "\r\n"+
    			"10.56.249.0/25"+"\r\n"+
    			"10.56.249.128/26"+"\r\n"+
    			"10.56.249.192/26"+"\r\n"+
    			"10.56.250.0/23";
			
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
		}
		
		void Button1Click(object sender, EventArgs e)
		{
			int i = 0;
			string [] ipcidr;
			if (richTextBox1.Lines.Length > 1){ lineas = richTextBox1.Lines.Length; }
			else richTextBox2.Text = "Nada que hacer";
			string [,] matriz = new string[lineas,4];
			char [] col1 = new char[lineas];
			char [] col2 = new char[lineas];
			char [] col3 = new char[lineas];
			char [] col4 = new char[lineas];
			char [] col5 = new char[lineas];
			char [] col6 = new char[lineas];
			char [] col7 = new char[lineas];
			char [] col8 = new char[lineas];
			char [] col9 = new char[lineas];
			char [] col10 = new char[lineas];
			char [] col11 = new char[lineas];
			char [] col12 = new char[lineas];
			char [] col13 = new char[lineas];
			char [] col14 = new char[lineas];
			char [] col15 = new char[lineas];
			char [] col16 = new char[lineas];
			char [] col17 = new char[lineas];
			char [] col18 = new char[lineas];
			char [] col19 = new char[lineas];
			char [] col20 = new char[lineas];
			char [] col21 = new char[lineas];
			char [] col22 = new char[lineas];
			char [] col23 = new char[lineas];
			char [] col24 = new char[lineas];
			char [] col25 = new char[lineas];
			char [] col26 = new char[lineas];
			char [] col27 = new char[lineas];
			char [] col28 = new char[lineas];
			char [] col29 = new char[lineas];
			char [] col30 = new char[lineas];
			char [] col31 = new char[lineas];
			char [] col32 = new char[lineas];
			int contador = 0;
			foreach (string linea in richTextBox1.Lines)
			{
				//1.3.5.0/8 - 123.123.123.128/22
				if (linea.Length >= 9 && linea.Length <=18)
				{
					ipcidr = linea.Split(new Char[] {'/'});
					i++;
					lineas = i;
					string[] octeto = ipcidr[0].Split(new Char[] { '.' });
					if (octeto.Length==4)
					{
						matriz [i-1,0] = DecimalToBinary(octeto[0]);
						col1 [i-1] = (matriz[i-1,0].Substring(0,1))[0];
						col2 [i-1] = (matriz[i-1,0].Substring(1,1))[0];
						col3 [i-1] = (matriz[i-1,0].Substring(2,1))[0];
						col4 [i-1] = (matriz[i-1,0].Substring(3,1))[0];
						col5 [i-1] = (matriz[i-1,0].Substring(4,1))[0];
						col6 [i-1] = (matriz[i-1,0].Substring(5,1))[0];
						col7 [i-1] = (matriz[i-1,0].Substring(6,1))[0];
						col8 [i-1] = (matriz[i-1,0].Substring(7,1))[0];
						matriz [i-1,1] = DecimalToBinary(octeto[1]);
						col9 [i-1] = (matriz[i-1,1].Substring(0,1))[0];
						col10 [i-1] = (matriz[i-1,1].Substring(1,1))[0];
						col11 [i-1] = (matriz[i-1,1].Substring(2,1))[0];
						col12 [i-1] = (matriz[i-1,1].Substring(3,1))[0];
						col13 [i-1] = (matriz[i-1,1].Substring(4,1))[0];
						col14 [i-1] = (matriz[i-1,1].Substring(5,1))[0];
						col15 [i-1] = (matriz[i-1,1].Substring(6,1))[0];
						col16 [i-1] = (matriz[i-1,1].Substring(7,1))[0];
						matriz [i-1,2] = DecimalToBinary(octeto[2]);
						col17 [i-1] = (matriz[i-1,2].Substring(0,1))[0];
						col18 [i-1] = (matriz[i-1,2].Substring(1,1))[0];
						col19 [i-1] = (matriz[i-1,2].Substring(2,1))[0];
						col20 [i-1] = (matriz[i-1,2].Substring(3,1))[0];
						col21 [i-1] = (matriz[i-1,2].Substring(4,1))[0];
						col22 [i-1] = (matriz[i-1,2].Substring(5,1))[0];
						col23 [i-1] = (matriz[i-1,2].Substring(6,1))[0];
						col24 [i-1] = (matriz[i-1,2].Substring(7,1))[0];
						matriz [i-1,3] = DecimalToBinary(octeto[3]);
						col25 [i-1] = (matriz[i-1,3].Substring(0,1))[0];
						col26 [i-1] = (matriz[i-1,3].Substring(1,1))[0];
						col27 [i-1] = (matriz[i-1,3].Substring(2,1))[0];
						col28 [i-1] = (matriz[i-1,3].Substring(3,1))[0];
						col29 [i-1] = (matriz[i-1,3].Substring(4,1))[0];
						col30 [i-1] = (matriz[i-1,3].Substring(5,1))[0];
						col31 [i-1] = (matriz[i-1,3].Substring(6,1))[0];
						col32 [i-1] = (matriz[i-1,3].Substring(7,1))[0];
					}
				}
			}
			
			for (int fila = 0; fila < lineas; fila++)
			{
				for (int j = 0; j < 4; j++)
    			{
    				richTextBox2.Text += matriz[fila,j];
    			}
    			richTextBox2.Text += "\r\n";
			}
			//sumarizar
			string s1="0",s2="0",s3="0",s4="0",s5="0",s6="0",s7="0",s8="0",
			s9="0",s10="0",s11="0",s12="0",s13="0",s14="0",s15="0",s16="0",
			s17="0",s18="0",s19="0",s20="0",s21="0",s22="0",s23="0",s24="0",
			s25="0",s26="0",s27="0",s28="0",s29="0",s30="0",s31="0",s32="0";
			i = 0;
			while (i < lineas -1 && col1[i].Equals(col1[i+1])) i++;
			if (i==(lineas-1)) 
			{
				contador=1;
				s1=col1[0].ToString();
				i = 0;
				if (sumarizar(col2)) {contador=2;s2=col2[0].ToString();
				 if (sumarizar(col3)) {contador=3;s3=col3[0].ToString();
				  if (sumarizar(col4)) {contador=4;s4=col4[0].ToString();
				   if (sumarizar(col5)) {contador=5;s5=col5[0].ToString();
				    if (sumarizar(col6)) {contador=6;s6=col6[0].ToString();
				     if (sumarizar(col7)) {contador=7;s7=col7[0].ToString();
				      if (sumarizar(col8)) {contador=8;s8=col8[0].ToString();
				       if (sumarizar(col9)) {contador=9;s9=col9[0].ToString();
					    if (sumarizar(col10)) {contador=10;s10=col10[0].ToString();
					     if (sumarizar(col11)) {contador=11;s11=col11[0].ToString();
					      if (sumarizar(col12)) {contador=12;s12=col12[0].ToString();
					       if (sumarizar(col13)) {contador=13;s13=col13[0].ToString();
					        if (sumarizar(col14)) {contador=14;s14=col14[0].ToString();
				             if (sumarizar(col5)) {contador=15;s15=col15[0].ToString();
					          if (sumarizar(col16)) {contador=16;s16=col16[0].ToString();
					           if (sumarizar(col17)) {contador=17;s17=col17[0].ToString();
					            if (sumarizar(col18)) {contador=18;s18=col18[0].ToString();
						         if (sumarizar(col19)) {contador=19;s19=col19[0].ToString();
						          if (sumarizar(col20)) {contador=20;s20=col20[0].ToString();
						           if (sumarizar(col21)) {contador=21;s21=col21[0].ToString();
						            if (sumarizar(col22)) {contador=22;s22=col22[0].ToString();
						             if (sumarizar(col23)) {contador=23;s23=col23[0].ToString();
						              if (sumarizar(col24)) {contador=24;s24=col24[0].ToString();
						               if (sumarizar(col25)) {contador=25;s25=col25[0].ToString();
							            if (sumarizar(col26)) {contador=26;s26=col26[0].ToString();
							             if (sumarizar(col27)) {contador=27;s27=col27[0].ToString();
						                  if (sumarizar(col28)) {contador=28;s28=col28[0].ToString();
							               if (sumarizar(col29)) {contador=29;s29=col29[0].ToString();
							                if (sumarizar(col30)) {contador=30;s30=col30[0].ToString();
							                 if (sumarizar(col31)) {contador=31;s31=col31[0].ToString();
							                  if (sumarizar(col32)) 
							                  {
							      			    contador=32;s32=col32[0].ToString();
					                          }
					                         }
					                        }
					                       }
					                      }
					                     }
					                    }
					                   }
					                  }
					                 }
					                }
					               }
					              }
					             }
					            }
					           }
					          }
					         }
					        }
					       }
					      }
					     }
					    }
				       }
				      }
				     }
				    }
				   }
				  }
				 }
				}
			}
			richTextBox2.Text += s1+s2+s3+s4+s5+s6+s7+s8+
				s9+s10+s11+s12+s13+s14+s15+s16+
				s17+s18+s19+s20+s21+s22+s23+s24+
				s25+s26+s27+s28+s29+s30+s31+s32+"/"+contador;
			string rutaIPa = s1+s2+s3+s4+s5+s6+s7+s8;
			string rutaIPb = s9+s10+s11+s12+s13+s14+s15+s16;
			string rutaIPc = s17+s18+s19+s20+s21+s22+s23+s24;
			string rutaIPd = s25+s26+s27+s28+s29+s30+s31+s32;
			try {
			rutaIPa = (Convert.ToInt16(rutaIPa,2)).ToString();
			rutaIPb = (Convert.ToInt16(rutaIPb,2)).ToString();
			rutaIPc = (Convert.ToInt16(rutaIPc,2)).ToString();
			rutaIPd = (Convert.ToInt16(rutaIPd,2)).ToString();
			} catch {}
			textBox1.Text = rutaIPa+"."+rutaIPb+"."+rutaIPc+"."+rutaIPd+
				"/"+contador.ToString();
		}
		public string DecimalToBinary(string data)
    	{
        string result = string.Empty;
        int rem = 0;
        try
        {
                int num = int.Parse(data);
                while (num > 0)
                {
                    rem = num % 2;
                    num = num / 2;
                    result = rem.ToString() + result;
                }
            
        }
        catch (Exception ex)
        {
        	richTextBox2.Text = ex.Message.ToString();
        }
        while (result.Length<8) 
        {
        	result = "0" + result;	
        }
        return result;
    	}
		
		public bool sumarizar(char [] unacol)
		{
			//
			int j = 0;
			while (j < lineas -1 && unacol[j].Equals(unacol[j+1])) j++;
			if (j==(lineas-1))  return true; else return false;
		}
		
		
		void Button2Click(object sender, EventArgs e)
		{
			richTextBox1.Text = "";
			richTextBox2.Text = "";
		}
	}
}
